<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\service\ActivityService;
use app\common\Common;
use app\common\StsService;
use app\model\PayRecord;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Request;
use think\Response;
use think\response\Json;
use Throwable;

class Activity extends Base
{
    /**
     * 显示资源列表
     *
     * @return Response
     */
    public function index()
    {
        return view();
    }

    /**
     * 列表接口
     * @return Json
     */
    public function list(): Json
    {
        try {
            $data  = ActivityService::getList(Request::param());
            return Common::success($data);
        } catch (\Throwable $e) {
            return Common::fail($e->getMessage());
        }
    }

    public function info()
    {
        return view();
    }

    /**
     * 删除.
     *
     * @return Response
     */
    public function delete()
    {
        $params = Request::param();
        try {
            $activity = \app\model\Activity::findOrFail($params['id']);
            if ($activity->delete()){
                return Common::success([],'删除成功');
            }
            return Common::fail('删除失败');
        } catch (Throwable $e) {
            return Common::fail($e->getMessage());
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return Response
     */
    public function add(Request $request)
    {
        $params = Request::param();
        try {
            validate(\app\admin\validate\Activity::class)->scene('add')->check($params);
            $adminUserInfo = $this->getUserInfo();
            $params['operator_id'] = $adminUserInfo['user_id'] ?? 0;
            $data = ActivityService::saveData($params);
            if (!empty($data['message'])) {
                return Common::fail($data['message']);
            }
            return Common::success($data);
        } catch (\Throwable $e) {
            flog([
                'message' => $e->getMessage(),
                'params' => $params,
            ],'admin_activity','error');
            return Common::fail($e->getMessage());
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return Response
     */
    public function detail($id)
    {
        $params = Request::param();

        try {
            validate(\app\admin\validate\Activity::class)->scene('detail')->check($params);

            $data = ActivityService::getInfo($params['id']);

            return Common::success($data);
        } catch (\Throwable $e) {
            return Common::fail($e->getMessage());
        }
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $params = Request::param();
        try {
            validate(\app\admin\validate\Activity::class)->scene('add')->check($params);
            $adminUserInfo = $this->getUserInfo();
            $params['operator_id'] = $adminUserInfo['user_id'] ?? 0;
            $data = ActivityService::saveData($params);
            if (!empty($data['message'])) {
                return Common::fail($data['message']);
            }
            return Common::success($data);
        } catch (\Throwable $e) {
            flog([
                'message' => $e->getMessage(),
                'params' => $params,
            ],'admin_activity','error');
            return Common::fail($e->getMessage());
        }
    }

    /**
     * 状态选择
     * @return Json
     */
    public function statusSelect()
    {
        $data = collect(\app\model\Activity::STATUS_ARR)->each(function ($value,$key){
            return ['key' => $key, 'value' => $value];
        })->values()->toArray();
        return Common::success($data);
    }

    /**
     * 类型选择
     * @return Json
     */
    public function typeSelect()
    {
        $data = collect(\app\model\Activity::TYPE_ARR)->each(function ($value,$key){
            return ['key' => $key, 'value' => $value];
        })->values()->toArray();
        return Common::success($data);
    }

    /**
     * 图片上传
     * @return Json
     */
    public function uploadImages()
    {
        $object = new StsService();
        $data = $object->getStsAuth();

        if ($data['statusCode'] != 200) {
            return Common::fail($data['errorMessage']);
        }

        return Common::success($data);
    }

    /**
     * 场景复选框
     * @return Json
     */
    public function sceneCheckbox(): Json
    {
        $data = collect(\app\model\Activity::SCENE_ARR)->each(function ($value,$key){
            return ['key' => $key, 'value' => $value];
        })->values()->toArray();
        return Common::success($data);
    }


    /**
     * 下拉列表
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function select(): Json
    {
        $data = \app\model\Activity::getDataByWhere(['status' => \app\model\Activity::STATUS_VALID]);
        return Common::success($data);
    }


    public function statistics()
    {
        return view();
    }

    //活动统计列表
    public function statisticsList()
    {
        try {
            $data  = ActivityService::getStatisticsList(Request::param());
            return Common::success($data);
        } catch (\Throwable $e) {
            return Common::fail($e->getMessage());
        }
    }
}
